<?php
session_start();
ob_start();
if (isset($_SESSION["id"]))
    header("Location: http://localhost:82/ttquang-web2/index.php?uid=" . $users[$index]['id']);
?>

<?php require_once 'model/users.php'; ?>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<form method="POST"> 
    <table>
        <tr>
            <td>Username: </td>
            <td><input type="text" name="username" id="name"></td>
        </tr>
        <tr>
            <td>Password: </td>
            <td><input type="password" name="password" id="password">
        </tr>
    </table>
    <input type="submit" name="submit" value="Enter"></p>
</form>

<?php
if (isset($_POST['submit'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $Ok = FALSE;
    $index = -1;
    for ($i = 0; $i < count($users); $i++) {
        if ((strcmp($users[$i]['username'], $username) == 0) &&
                (strcmp($users[$i]['password'], $password) == 0)) {
            $Ok = TRUE;
            $index = $i;
        }
    }
    if ($Ok) {
        $_SESSION["id"] = $users[$index]["id"];
        header("Location: http://localhost:82/ttquang-web2/index.php");
    } else {
        echo "Đăng nhập thất bại";
    }
}
?>

<?php ob_end_flush(); ?>